package org.jsbd.boss.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.dao.IChannelApkInfoDAO;
import org.jsbd.boss.dao.ImsiLogDAO;
import org.jsbd.boss.domian.ChannelApkInfo;
import org.jsbd.boss.domian.channel.ChannelApkConfig;
import org.jsbd.boss.domian.channel.ImsiApkLog;
import org.jsbd.boss.service.ClientManager;
import org.jsbd.boss.service.IChannelApkInfoService;
import org.jsbd.boss.service.ImsiLogService;
import org.jsbd.boss.util.DateUtil;
import org.jsbd.boss.util.PropertiesUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class ImsiLogServiceImpl implements ImsiLogService {
	public static final Logger LOGGER = LoggerFactory.getLogger(ImsiLogServiceImpl.class);

	@Resource
	private ImsiLogDAO imsiLogDao;

	@Resource
	private ClientManager clientManager;
	@Resource
	private IChannelApkInfoService channelApkInfoService;

	@Resource
	private IChannelApkInfoDAO channelApkInfoDAO;

	@Override
	public void updateImsiLog() {
		List<String> dayList = getDayList();
		for (int i = 0; i < dayList.size(); i++) {
			String day = dayList.get(i);
			List<ImsiApkLog> apkLogs = imsiLogDao.selectApkLogs(day);
			LOGGER.info("执行更新{}对应的数据", day);
			while (apkLogs != null && apkLogs.size() > 0) {
				for (ImsiApkLog apkLog : apkLogs) {
					apkLog.setCurrentDate(day);
					try {
						String mark = apkLog.getMark();
						String imsi = apkLog.getImsi();
						LOGGER.info("执行更新{} , imsi{}, mark{} 对应的数据", new Object[] { day, imsi, mark });
						long time = apkLog.getCreateTime().getTime();
						ChannelApkConfig apkConfig = channelApkInfoService.selectChannelApkConfig(mark);
						Integer apkId = null;
						Integer base = null;
						if (apkConfig != null && apkConfig.getId() != null) {
							apkId = apkConfig.getId();
							base = apkConfig.getBase();
						} else {
							ChannelApkInfo channelApkInfo = channelApkInfoDAO.getConfigByMark(mark);
							if (channelApkInfo != null) {
								String base1 = channelApkInfo.getBase();
								if (StringUtils.isNotBlank(base1)) {
									base = Integer.parseInt(base1);
								}
							}
						}
						if (!clientManager.hasLogined(imsi, apkId, base)) {
							// 没登录就登录
							clientManager.login(imsi, apkId, base, time);
						}
						// apkLog.setBase(base);
					} catch (Exception e) {
						LOGGER.error("更新imsi执行记录出错", e);
					}
				}
				imsiLogDao.updateFlag(apkLogs);
				apkLogs = imsiLogDao.selectApkLogs(day);
			}
		}
	}

	private List<String> getDayList() {
		List<String> dayList = new ArrayList<String>();
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date startDate;
		try {
			startDate = sdf.parse(PropertiesUtil.getValue("imsi_start"));
			Date endDate = sdf.parse(DateUtil.getCurrentDateStr());
			cal.setTime(startDate);
			boolean bContinue = true;
			while (bContinue) {
				cal.add(Calendar.DAY_OF_MONTH, 1);
				if (endDate.after(cal.getTime())) {
					dayList.add(sdf.format(cal.getTime()));
				} else {
					break;
				}
			}
		} catch (ParseException e) {
		}
		return dayList;
	}
}
